﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using DevExpress.Utils;
using DevEx.Thanh.Class;

namespace DevEx.QuocSy.UserControl
{
    public partial class ucChuyenKhoNoiBo : DevExpress.XtraEditors.XtraUserControl
    {
        private int _ID_User = GlobalVar.CurrentUser.ID_User;
        private int _ID_Kho = Convert.ToInt32(GlobalVar.CurrentUser.ID_Kho);
        private int _ID_PhieuXuat;
        private int _ID_PhieuNhap;
        private int _SoPhieuNhap;
        private int _SoPhieuXuat;

        CDPNX m_DPNX = new CDPNX();
        QLVTDataContext dc = new QLVTDataContext();
        DataTable dtSLNX;

        public ucChuyenKhoNoiBo()
        {
            InitializeComponent();
            KhoiTaoDM();
            dtpNgayPhieu.EditValue = DateTime.Now;
            lueKhoXuat.EditValue = _ID_Kho;

            CUserController m_user = new CUserController();
            lblNguoiLapPhieu.Text = "Người lập phiếu: " + m_user.SelectRecordByID(_ID_User).FullName;

            KhoiTaodtSLNX();
            KhoiTaoSLNX(0);
            FormatdtgPhieuXuat();
            KhoiTaoGiaoDien(false);
            KhoiTaoSoPhieuXuat();
            KhoiTaoSoPhieuNhap();
        }

        public ucChuyenKhoNoiBo(string NX, int ID_DPNX)
        {
            //
            InitializeComponent();
            KhoiTaoDM();

            int ID_PhieuXuat;
            int ID_PhieuNhap;

            if (NX == "N")
            {
                ID_PhieuNhap = ID_DPNX;
                ID_PhieuXuat = Convert.ToInt32((from p in dc.tblCKNBs
                                                where p.ID_PhieuNhap == ID_DPNX
                                                select p.ID_PhieuXuat).SingleOrDefault());
            }
            else 
            {
                ID_PhieuXuat = ID_DPNX;
                ID_PhieuNhap = Convert.ToInt32((from p in dc.tblCKNBs
                                                where p.ID_PhieuXuat == ID_DPNX
                                                select p.ID_PhieuNhap).SingleOrDefault());
            }

            _ID_PhieuNhap = ID_PhieuNhap;
            _ID_PhieuXuat = ID_PhieuXuat;


            CDPNX m_DPNX = new CDPNX();
            tblDPNX obj_PhieuXuat = m_DPNX.SelectRecordByID(ID_PhieuXuat);
            tblDPNX obj_PhieuNhap = m_DPNX.SelectRecordByID(ID_PhieuNhap);
            
            

            CUserController m_user = new CUserController();
            lblNguoiLapPhieu.Text = "Người lập phiếu: " + m_user.SelectRecordByID(_ID_User).FullName;


            KhoiTaodtSLNX();
            KhoiTaoSLNX(ID_PhieuXuat);
            FormatdtgPhieuXuat();
            KhoiTaoGiaoDien(true);

            CModule_Sy m_module = new CModule_Sy();

            txtSoPhieuNhap.Text = m_module.GetSoPhieu(ID_PhieuNhap);//Convert.ToString (obj_PhieuNhap.SoPhieu);
            txtSoPhieuXuat.Text = m_module.GetSoPhieu(ID_PhieuXuat); //Convert.ToString(obj_PhieuXuat.SoPhieu);
            _SoPhieuNhap = Convert.ToInt32(obj_PhieuNhap.SoPhieu);
            _SoPhieuXuat = Convert.ToInt32(obj_PhieuXuat.SoPhieu);

            lueKhoNhap.EditValue = Convert.ToInt32(obj_PhieuNhap.ID_Kho);
            lueKhoXuat.EditValue = Convert.ToInt32(obj_PhieuXuat.ID_Kho);
            txtNguoiGiao.Text = Convert.ToString(obj_PhieuXuat.NguoiGiaoNhan);
            txtNguoiNhan.Text = Convert.ToString(obj_PhieuNhap.NguoiGiaoNhan);

            dtpNgayPhieu.EditValue = obj_PhieuXuat.NgayPhieu;           
            
            //KhoiTaoSoPhieuXuat();
            //KhoiTaoSoPhieuNhap();
        }

        public void KhoiTaoDM()
        {
           
            //2. Danh muc Kho
            CKho m_khoxuat = new CKho();
            IQueryable<tblKho> list_khoxuat = m_khoxuat.SelectAllRecord();

            lueKhoXuat.Properties.DisplayMember = "Kho";
            lueKhoXuat.Properties.ValueMember = "ID_Kho";

            lueKhoXuat.Properties.DataSource = list_khoxuat;
            lueKhoXuat.Properties.PopulateColumns();

            CKho m_khonhap = new CKho();
            IQueryable<tblKho> list_khonhap = m_khonhap.SelectAllRecord();

            lueKhoNhap.Properties.DisplayMember = "Kho";
            lueKhoNhap.Properties.ValueMember = "ID_Kho";

            lueKhoNhap.Properties.DataSource = list_khonhap;
            lueKhoNhap.Properties.PopulateColumns();

           
        }

        private void dtgPhieuXuat_DoubleClick(object sender, EventArgs e)
        {
            if (btnGhi.Enabled)
            {
                int ID_Kho = 0;
                try { ID_Kho = Convert.ToInt32(lueKhoXuat.EditValue); }
                catch { }
                if (ID_Kho == 0)
                {
                    MessageBox.Show("Bạn chưa chọn Kho cần làm phiếu xuất");
                    lueKhoXuat.Focus();
                    return;
                }
                XtraForm frmTon = new frmHangTon(ID_Kho, this);
                frmTon.ShowDialog();
            }      
        }

        public void CreatNewRowTon(int ID_MatHang, double SLKH)
        {
            gridView1.AddNewRow();

            int rowHandle = gridView1.GetRowHandle(gridView1.DataRowCount);
            if (gridView1.IsNewItemRow(rowHandle))
            {
                CTon m_ton = new CTon();
                tblTon obj_ton = m_ton.SelectRecordByID(ID_MatHang);

                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ID_MatHang"], ID_MatHang);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["VatTu"], obj_ton.tblVatTu.VatTu);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ID_CCL"], obj_ton.ID_CCL);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ID_NguonVatTu"], obj_ton.ID_NguonVatTu);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ID_HopDong"], obj_ton.ID_HopDong);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["Gia"], obj_ton.Gia);                
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["SLKH"], SLKH);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["NamSX"], obj_ton.NamSX);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["ThanhTien"], obj_ton.Gia * SLKH);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["MaKyHieu"], obj_ton.tblVatTu.MaKyHieu);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["SoTheKho"], obj_ton.SoTheKho);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["TangKeKhoang"], obj_ton.TangKeKhoang);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["GhiChu"], "");
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["Status"], 1);
                gridView1.SetRowCellValue(rowHandle, gridView1.Columns["Del"], false);

                gridView1.UpdateCurrentRow();
            }
        }

        public void KhoiTaodtSLNX()
        {
            dtSLNX = new DataTable();
            dtSLNX.Columns.Add("Del", typeof(Boolean));
            dtSLNX.Columns.Add("ID_SLNX", typeof(int));
            dtSLNX.Columns.Add("ID_MatHang", typeof(int));
            dtSLNX.Columns.Add("ID_VatTu", typeof(int));
            dtSLNX.Columns.Add("VatTu", typeof(String));
            dtSLNX.Columns.Add("Code", typeof(String));
            dtSLNX.Columns.Add("MaKyHieu", typeof(String));
            dtSLNX.Columns.Add("NamSX", typeof(String));
            dtSLNX.Columns.Add("ID_CCL", typeof(int));
            dtSLNX.Columns.Add("Gia", typeof(Double));            
            dtSLNX.Columns.Add("SLKH", typeof(Double));
            dtSLNX.Columns.Add("ThanhTien", typeof(Double));
            dtSLNX.Columns.Add("ID_NguonVatTu", typeof(int));
            dtSLNX.Columns.Add("ID_HopDong", typeof(int));
            dtSLNX.Columns.Add("SoTheKho", typeof(String));
            dtSLNX.Columns.Add("TangKeKhoang", typeof(String));
            dtSLNX.Columns.Add("GhiChu", typeof(String));
            dtSLNX.Columns.Add("Status", typeof(int));//0: binh thuong, 1: them, 2: sua, 3: xoa
        }

        public void KhoiTaoSLNX(int ID_DPNX)
        {
            //CSLNX m_SLNX = new CSLNX();
            //dtgPhieuNhap.DataSource = m_SLNX.SelectAllRecord(ID_DPNX);

            var q = from p in dc.tblSLNXes
                    where p.ID_DPNX == ID_DPNX
                    select new
                    {
                        p.ID_SLNX,
                        p.ID_MatHang,
                        p.tblTon.ID_VatTu,
                        p.tblTon.tblVatTu.VatTu,
                        p.tblTon.Code,
                        p.tblTon.tblVatTu.MaKyHieu,
                        p.tblTon.NamSX,
                        p.tblTon.ID_CCL,
                        p.tblTon.ID_NguonVatTu,
                        p.tblTon.ID_HopDong,
                        p.tblTon.Gia,                        
                        p.SLKH,
                        ThanhTien = p.SLKH * p.tblTon.Gia,
                        p.tblTon.SoTheKho,
                        p.tblTon.TangKeKhoang,
                        p.GhiChu
                    };

            dtSLNX.Clear();

            foreach (var ton in q)
            {
                DataRow row = dtSLNX.NewRow();
                row["ID_SLNX"] = ton.ID_SLNX;
                row["ID_MatHang"] = ton.ID_MatHang;
                row["ID_VatTu"] = ton.ID_VatTu;
                row["VatTu"] = ton.VatTu;
                row["Code"] = ton.Code;
                row["MaKyHieu"] = ton.MaKyHieu;
                row["ID_CCL"] = ton.ID_CCL;
                row["Gia"] = ton.Gia;                
                row["NamSX"] = ton.NamSX ?? "";
                row["SLKH"] = ton.SLKH ?? 0;
                row["ThanhTien"] = ton.ThanhTien;
                row["ID_NguonVatTu"] = ton.ID_NguonVatTu;

                if (ton.ID_HopDong == null)
                    row["ID_HopDong"] = DBNull.Value;
                else
                    row["ID_HopDong"] = ton.ID_HopDong;

                row["SoTheKho"] = ton.SoTheKho;
                row["TangKeKhoang"] = ton.TangKeKhoang;
                row["GhiChu"] = ton.GhiChu;
                row["Status"] = 0;//binh thuong
                row["Del"] = 0;//binh thuong
                dtSLNX.Rows.Add(row);
            }

            dtgPhieuXuat.DataSource = dtSLNX;
        }

        public void FormatdtgPhieuXuat()
        {
            gridView1.OptionsView.ShowFooter = true;
            gridView1.Columns.ColumnByFieldName("ID_SLNX").Visible = false;
            gridView1.Columns.ColumnByFieldName("ID_VatTu").Visible = false;
            gridView1.Columns.ColumnByFieldName("ID_MatHang").Visible = false;
            gridView1.Columns.ColumnByFieldName("Status").Visible = false;
            gridView1.Columns.ColumnByFieldName("Code").Visible = false;

            //Columns Del
            var cl_Del = gridView1.Columns.ColumnByFieldName("Del");
            cl_Del.Width = 30;
            cl_Del.Caption = "Xóa";
            cl_Del.OptionsColumn.AllowEdit = false;
            cl_Del.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Del.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns VatTu
            var cl_VatTu = gridView1.Columns.ColumnByFieldName("VatTu");
            cl_VatTu.Width = 150;
            cl_VatTu.Caption = "Vật Tư";
            cl_VatTu.OptionsColumn.AllowEdit = false;
            cl_VatTu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_VatTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns CCL
            var cl_CCL = gridView1.Columns.ColumnByFieldName("ID_CCL");
            cl_CCL.Width = 50;
            cl_CCL.Caption = "CCL";
            cl_CCL.OptionsColumn.AllowEdit = false;
            cl_CCL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_CCL.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns Nguon Tai Chinh
            var cl_NguonVatTu = gridView1.Columns.ColumnByFieldName("ID_NguonVatTu");
            cl_NguonVatTu.Width = 80;
            cl_NguonVatTu.Caption = "N. TC";
            cl_NguonVatTu.OptionsColumn.AllowEdit = false;
            cl_NguonVatTu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NguonVatTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns Nguon Nhap
            var cl_NguonNhap = gridView1.Columns.ColumnByFieldName("ID_HopDong");
            cl_NguonNhap.Width = 80;
            cl_NguonNhap.Caption = "N. Nhập";
            cl_NguonNhap.OptionsColumn.AllowEdit = false;
            cl_NguonNhap.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NguonNhap.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //format Nguon Tai Chinh
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repositoryItemLookUpEdit_NguonVatTu = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            CNguonVatTu m_nvt = new CNguonVatTu();
            repositoryItemLookUpEdit_NguonVatTu.DataSource = m_nvt.SelectAllRecord();

            repositoryItemLookUpEdit_NguonVatTu.DisplayMember = "NguonVatTu";
            repositoryItemLookUpEdit_NguonVatTu.ValueMember = "ID_NguonVatTu";
            repositoryItemLookUpEdit_NguonVatTu.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo("NguonVatTu", "NguonVatTu", 20, DevExpress.Utils.FormatType.None, "", true, DevExpress.Utils.HorzAlignment.Default, DevExpress.Data.ColumnSortOrder.None)});
            //repositoryItemLookUpEdit_NguonVatTu.Appearance.Font = new Font(this.Appearance.Font.Name, 10);
            cl_NguonVatTu.ColumnEdit = repositoryItemLookUpEdit_NguonVatTu;

            //format Nguon Nhap
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repositoryItemLookUpEdit_NguonNhap = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            CHopDong m_HopDong = new CHopDong();
            repositoryItemLookUpEdit_NguonNhap.DataSource = m_nvt.SelectAllRecord();

            repositoryItemLookUpEdit_NguonNhap.DisplayMember = "SoHopDong";
            repositoryItemLookUpEdit_NguonNhap.ValueMember = "ID_HopDong";
            repositoryItemLookUpEdit_NguonNhap.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo("SoHopDong", "SoHopDong", 20, DevExpress.Utils.FormatType.None, "", true, DevExpress.Utils.HorzAlignment.Default, DevExpress.Data.ColumnSortOrder.None)});
            //repositoryItemLookUpEdit_NguonVatTu.Appearance.Font = new Font(this.Appearance.Font.Name, 10);
            cl_NguonNhap.ColumnEdit = repositoryItemLookUpEdit_NguonNhap;

            //Columns NamSX
            var cl_NamSX = gridView1.Columns.ColumnByFieldName("NamSX");
            cl_NamSX.Width = 50;
            cl_NamSX.OptionsColumn.AllowEdit = false;
            cl_NamSX.Caption = "Năm SX";
            cl_NamSX.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_NamSX.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns MaKyHieu
            var cl_MaKyHieu = gridView1.Columns.ColumnByFieldName("MaKyHieu");
            cl_MaKyHieu.Caption = "Mã Hiệu";
            cl_MaKyHieu.Width = 70;
            cl_MaKyHieu.OptionsColumn.AllowEdit = false;
            cl_MaKyHieu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_MaKyHieu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns SoTheKho
            var cl_SoTheKho = gridView1.Columns.ColumnByFieldName("SoTheKho");
            cl_SoTheKho.Width = 60;
            cl_SoTheKho.OptionsColumn.AllowEdit = false;
            cl_SoTheKho.Caption = "Thẻ Kho";
            cl_SoTheKho.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            cl_SoTheKho.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns TangKeKhoang
            var cl_TangKeKhoang = gridView1.Columns.ColumnByFieldName("TangKeKhoang");
            cl_TangKeKhoang.Width = 80;
            cl_TangKeKhoang.OptionsColumn.AllowEdit = false;
            cl_TangKeKhoang.Caption = "Vị trí";
            cl_TangKeKhoang.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_TangKeKhoang.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Columns GhiChu
            var cl_GhiChu = gridView1.Columns.ColumnByFieldName("GhiChu");
            cl_GhiChu.Caption = "Ghi chú";
            cl_GhiChu.AppearanceCell.BackColor = Color.Azure;
            cl_GhiChu.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_GhiChu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;


            //Columns Gia
            var cl_Gia = gridView1.Columns.ColumnByFieldName("Gia");
            cl_Gia.Width = 70;
            cl_Gia.Caption = "Đơn giá";
            cl_Gia.OptionsColumn.AllowEdit = false;
            cl_Gia.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_Gia.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_Gia.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_Gia.DisplayFormat.FormatString = "n0";

            //Columns ThanhTien           
            var cl_ThanhTien = gridView1.Columns["ThanhTien"];
            cl_ThanhTien.Width = 80;
            cl_ThanhTien.OptionsColumn.AllowEdit = false;
            cl_ThanhTien.Caption = "Thành tiền";
            cl_ThanhTien.Summary.Add(DevExpress.Data.SummaryItemType.Sum, "ThanhTien", "{0:n0}");
            cl_ThanhTien.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_ThanhTien.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_ThanhTien.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_ThanhTien.DisplayFormat.FormatString = "#,###.#";

           
            //Columns SLTonKH
            var cl_SLTonKH = gridView1.Columns.ColumnByFieldName("SLKH");
            cl_SLTonKH.Width = 60;
            cl_SLTonKH.AppearanceCell.BackColor = Color.Azure;
            cl_SLTonKH.Caption = "SLKH";
            cl_SLTonKH.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            cl_SLTonKH.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            cl_SLTonKH.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            cl_SLTonKH.DisplayFormat.FormatString = "#,###.#";

            //repositoryItemLookUpEdit1.AutoHeight = false;
            //repositoryItemLookUpEdit1.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            //new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            //repositoryItemLookUpEdit1.DropDownRows = 10;
            //repositoryItemLookUpEdit1.Name = "repositoryItemLookUpEdit1";
            //repositoryItemLookUpEdit1.PopupWidth = 220;

            //format CCL
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repositoryItemLookUpEdit_CCL = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            CCCL m_ccl = new CCCL();
            repositoryItemLookUpEdit_CCL.DataSource = m_ccl.SelectAllRecord();

            repositoryItemLookUpEdit_CCL.DisplayMember = "CCL";
            repositoryItemLookUpEdit_CCL.ValueMember = "ID_CCL";
            repositoryItemLookUpEdit_CCL.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo("CCL", "CCL", 20, DevExpress.Utils.FormatType.None, "", true, DevExpress.Utils.HorzAlignment.Default, DevExpress.Data.ColumnSortOrder.None)});
            ((DevExpress.XtraGrid.Views.Base.ColumnView)dtgPhieuXuat.MainView).Columns["ID_CCL"].ColumnEdit = repositoryItemLookUpEdit_CCL;
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            //A. PHIEU XUAT
            //1.  Kiểm tra dữ liệu
            //1.1 Kiểm tra chọn Kho
            if (lueKhoNhap.EditValue == null)
            {
                MessageBox.Show("Bạn chưa chọn Kho");
                lueKhoNhap.Focus();
                return;
            }
            ////1.2 Kiểm tra chọn TCNX và Sản phẩm
            //if (lueTCNX.EditValue == null)
            //{
            //    MessageBox.Show("Bạn chưa chọn Tính chất xuất");
            //    lueTCNX.Focus();
            //    return;
            //}
            //else
            //{
            //    if (lueTCNX.Text == "Xuất sửa chữa")
            //    {
            //        if (lueSanPham.EditValue == null)
            //        {
            //            MessageBox.Show("Bạn chưa chọn Sản phẩm để xuất sửa chữa");
            //            lueSanPham.Focus();
            //            return;
            //        }
            //    }
            //}
            //1.3 Kiểm tra DVGN
            //if (_ID_DonVi == 0)
            //{
            //    MessageBox.Show("Bạn chưa chọn Đơn vị nhận hàng");
            //    txtDonVi.Focus();
            //    return;
            //}


            // Tính tổng giá trị phiếu xuất
            Double TongGiaTri = 0;
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                int status = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["Status"]));
                if (status != 3)
                {
                    Double Gia = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["Gia"]));
                    Double SLKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));
                    TongGiaTri += Gia * SLKH;
                }
            }
            //2. DPNX
            tblDPNX obj_DPNX = new tblDPNX();
            CDPNX m_DPNX = new CDPNX();
            if (_ID_PhieuXuat == 0)
            {
                //var obj_DPNX = m_DPNX.SelectRecordByID(_ID_DPNX);

                obj_DPNX.HoaDon = "";
                obj_DPNX.NX = "X";
                obj_DPNX.LyDoNhapXuat = "Chuyển kho nội bộ";
                obj_DPNX.NguoiGiaoNhan = Convert.ToString(txtNguoiGiao.EditValue ?? "");
                obj_DPNX.NgayPhieu = Convert.ToDateTime(dtpNgayPhieu.EditValue);
                obj_DPNX.ID_Kho = Convert.ToInt32(lueKhoXuat.EditValue);
                obj_DPNX.ID_TCNX = 9;
                obj_DPNX.ID_CreatedUser = _ID_User;
                //string[] SoPhieu;
                //SoPhieu = Convert.ToString(txtSoPhieuXuat.EditValue ?? "").Split('/');
                obj_DPNX.SoPhieu = _SoPhieuXuat;//Convert.ToInt32(SoPhieu[0]);
                obj_DPNX.DuoiPhieu = "CK";

                obj_DPNX.ID_SanPham = null;

                CKho m_Kho = new CKho();
                obj_DPNX.ID_DonVi = Convert.ToInt32(m_Kho.SelectRecordByID(Convert.ToInt32(lueKhoNhap.EditValue)).ID_DonVi); 
                obj_DPNX.TongGiaTri = TongGiaTri;
                _ID_PhieuXuat = m_DPNX.Insert(obj_DPNX);
            }
            else
            {
                obj_DPNX.ID_DPNX = _ID_PhieuXuat;
                obj_DPNX.HoaDon = "";
                obj_DPNX.NX = "X";
                obj_DPNX.LyDoNhapXuat = "Chuyển kho nội bộ";
                obj_DPNX.NguoiGiaoNhan = Convert.ToString(txtNguoiGiao.EditValue ?? "");
                obj_DPNX.NgayPhieu = Convert.ToDateTime(dtpNgayPhieu.EditValue);
                obj_DPNX.ID_Kho = Convert.ToInt32(lueKhoXuat.EditValue);
                obj_DPNX.ID_TCNX = 9;
                obj_DPNX.ID_CreatedUser = _ID_User;
                //string[] SoPhieu;
                //SoPhieu = Convert.ToString(txtSoPhieuXuat.EditValue ?? "").Split('/');
                obj_DPNX.SoPhieu = _SoPhieuXuat;//Convert.ToInt32(SoPhieu[0]);
                obj_DPNX.DuoiPhieu = "CK";
                
                obj_DPNX.ID_SanPham = null;

                CKho m_Kho = new CKho();
                obj_DPNX.ID_DonVi = Convert.ToInt32(m_Kho.SelectRecordByID(Convert.ToInt32(lueKhoNhap.EditValue)).ID_DonVi); 

                obj_DPNX.TongGiaTri = TongGiaTri;

                m_DPNX.Update(obj_DPNX);
            }



            // 3. Chi Tiet Nhap Xuat (SLNX)
            // 3.1 Get ID_MatHang 
            // 3.1.1 Nếu tồn tại ID_MatHang trong tồn
            // 3.1.2 Ngược lại không có trong tồn -> tạo tồn trước
            // 3.2 Update SLNX và tồn


            // Kiem tra sua phieu hay them
            // Neu sua phieu thi xoa phieu truoc, them lai phieu sau.
            CTon m_ton = new CTon();
            CSLNX m_SLNX = new CSLNX();


            if (_ID_PhieuXuat != 0)
            { 
                //1. Xóa chi tiết phiếu xuất
                var list_CTPhieuXuat = m_SLNX.SelectAllRecord(_ID_PhieuXuat);
                foreach(tblSLNX objSLNX in list_CTPhieuXuat)
                {
                    //1. Xóa SLNX
                    m_SLNX.Delete(objSLNX.ID_SLNX);

                    //2. Xóa trong Tồn (SLTonKH = SLTonKH + SLKH)
                    m_ton.DeleteXuat(Convert.ToInt32(objSLNX.ID_MatHang) , Convert.ToDouble(objSLNX.SLKH));
                }


                //2. Xóa chi tiết phiếu nhập
                var list_CTPhieuNhap = m_SLNX.SelectAllRecord(_ID_PhieuNhap);
                foreach (tblSLNX objSLNX in list_CTPhieuNhap)
                {
                    //1. Xóa SLNX
                    m_SLNX.Delete(objSLNX.ID_SLNX);

                    //2. Xóa trong Tồn (SLTonKH = SLTonKH - SLKH)
                    m_ton.DeleteNhap(Convert.ToInt32(objSLNX.ID_MatHang), Convert.ToDouble(objSLNX.SLKH));              
                }
            }

            for (int i = 0; i < gridView1.RowCount; i++)
            {

                int ID_MatHang = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_MatHang"]));
                double Gia = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["Gia"]));

                int status = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["Status"]));
                if (status != 3)
                { //Thêm or Sửa

                    double SLKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));
                    m_ton.InsertXuat(ID_MatHang, SLKH);

                    tblSLNX objSLNX = new tblSLNX();
                    objSLNX.ID_MatHang = ID_MatHang;
                    objSLNX.ID_DPNX = obj_DPNX.ID_DPNX;
                    objSLNX.SLKH = SLKH;
                    objSLNX.ThanhTien = SLKH * Gia;
                    objSLNX.GhiChu = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["GhiChu"]) ?? "");

                    m_SLNX.Insert(objSLNX);
                }


                ////Xóa
                //if (status == 3)
                //{
                //    int ID_SLNX = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_SLNX"]));
                //    double SLKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));

                //    //1. Xóa SLNX
                //    m_SLNX.Delete(ID_SLNX);

                //    //2. Xóa trong Tồn (SLTonKH = SLTonKH + SLKH)
                //    m_ton.DeleteXuat(ID_MatHang, SLKH);

                //}
                ////Sửa
                //if (status == 2)
                //{
                //    int ID_SLNX = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_SLNX"]));
                //    tblSLNX objSLNX = m_SLNX.SelectRecordByID(ID_SLNX);
                //    double SLKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));
                //    //Update tblTon voi SLKH_Old, SLKH_New
                //    m_ton.UpdateXuat(ID_MatHang, Convert.ToDouble(objSLNX.SLKH), SLKH);

                //    tblSLNX _SLNX = new tblSLNX();
                //    _SLNX.ID_SLNX = ID_SLNX;
                //    _SLNX.ID_MatHang = ID_MatHang;
                //    _SLNX.ID_DPNX = _ID_PhieuXuat;
                //    _SLNX.SLKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));
                //    _SLNX.GhiChu = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["GhiChu"]) ?? "");
                //    _SLNX.ThanhTien = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["ThanhTien"]));
                //    m_SLNX.Update(_SLNX);
                //}
            }
            //KhoiTaoSLNX(obj_DPNX.ID_DPNX);
            //KhoiTaoGiaoDien(true);

            
            
            //B. PHIEU NHAP--------------------------------------------------------------------------------------------------------
            //Double TongGiaTri = 0;
            //for (int i = 0; i < gridView1.RowCount; i++)
            //{
            //    int status = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["Status"]));
            //    if (status != 3)
            //    {
            //        Double Gia = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["Gia"]));
            //        Double SLKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));
            //        TongGiaTri += Gia * SLKH;
            //    }
            //}
            //2. DPNX
            tblDPNX obj_PhieuNhap = new tblDPNX();
            //CDPNX m_DPNX = new CDPNX();
            if (_ID_PhieuNhap == 0)
            {
                //var obj_DPNX = m_DPNX.SelectRecordByID(_ID_DPNX);

                obj_PhieuNhap.HoaDon = "";
                obj_PhieuNhap.NX = "N";
                obj_PhieuNhap.LyDoNhapXuat = "Chuyển kho nội bộ";
                obj_PhieuNhap.NguoiGiaoNhan = Convert.ToString(txtNguoiNhan.EditValue ?? "");
                obj_PhieuNhap.NgayPhieu = Convert.ToDateTime(dtpNgayPhieu.EditValue);
                obj_PhieuNhap.ID_Kho = Convert.ToInt32(lueKhoNhap.EditValue);
                obj_PhieuNhap.ID_TCNX = 10; // chuyen kho noi bo
                //string[] SoPhieu;
                //SoPhieu = Convert.ToString(txtSoPhieuNhap.EditValue ?? "").Split('/');
                obj_PhieuNhap.SoPhieu = _SoPhieuNhap;//Convert.ToInt32(SoPhieu[0]);
                obj_PhieuNhap.DuoiPhieu = "CK";

                CKho m_Kho = new CKho();
                obj_PhieuNhap.ID_DonVi = Convert.ToInt32(m_Kho.SelectRecordByID(Convert.ToInt32(lueKhoXuat.EditValue)).ID_DonVi);

                obj_PhieuNhap.TongGiaTri = TongGiaTri;
                obj_PhieuNhap.ID_CreatedUser = _ID_User;

                _ID_PhieuNhap = m_DPNX.Insert(obj_PhieuNhap);

                CCKNB m_CKNB = new CCKNB();
                tblCKNB obj_CKNB = new tblCKNB();
                obj_CKNB.ID_PhieuNhap = _ID_PhieuNhap;
                obj_CKNB.ID_PhieuXuat = _ID_PhieuXuat;
                m_CKNB.Insert(obj_CKNB);
            }
            else
            {
                obj_PhieuNhap.ID_DPNX = _ID_PhieuNhap;
                obj_PhieuNhap.HoaDon = "";
                obj_PhieuNhap.NX = "N";
                obj_PhieuNhap.LyDoNhapXuat = "Chuyển kho nội bộ";
                obj_PhieuNhap.NguoiGiaoNhan = Convert.ToString(txtNguoiNhan.EditValue ?? "");
                obj_PhieuNhap.NgayPhieu = Convert.ToDateTime(dtpNgayPhieu.EditValue);
                obj_PhieuNhap.ID_Kho = Convert.ToInt32(lueKhoNhap.EditValue);
                obj_PhieuNhap.ID_TCNX = 10; // chuyen kho noi bo
                //string[] SoPhieu;
                //SoPhieu = Convert.ToString(txtSoPhieuNhap.EditValue ?? "").Split('/');
                obj_PhieuNhap.SoPhieu = _SoPhieuNhap;// Convert.ToInt32(SoPhieu[0]);
                obj_PhieuNhap.DuoiPhieu = "CK";

                CKho m_Kho = new CKho();
                obj_PhieuNhap.ID_DonVi = Convert.ToInt32(m_Kho.SelectRecordByID(Convert.ToInt32(lueKhoXuat.EditValue)).ID_DonVi);

                obj_PhieuNhap.TongGiaTri = TongGiaTri;
                obj_PhieuNhap.ID_CreatedUser = _ID_User;

                m_DPNX.Update(obj_PhieuNhap);
            }

            // 3. Chi Tiet Nhap Xuat (SLNX)
            // 3.1 Get ID_MatHang 
            // 3.1.1 Nếu tồn tại ID_MatHang trong tồn
            // 3.1.2 Ngược lại không có trong tồn -> tạo tồn trước
            // 3.2 Update SLNX và tồn

            //CTon m_ton = new CTon();
            //CSLNX m_SLNX = new CSLNX();

            for (int i = 0; i < gridView1.RowCount; i++)
            {
                int status = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["Status"]));
                if (status != 3)
                { //Thêm or sửa
                    int ID_MatHang = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_MatHang"]));
                    CTon m_tonNhap = new CTon();
                    tblTon tonNhap = m_tonNhap.SelectRecordByID(ID_MatHang);

                    tblTon ton = new tblTon();
                    ton.ID_VatTu = tonNhap.ID_VatTu;                    
                    ton.ID_Kho = Convert.ToInt32(lueKhoNhap.EditValue);
                    ton.ID_CCL = tonNhap.ID_CCL;
                    ton.ID_NguonVatTu = tonNhap.ID_NguonVatTu;
                    ton.NamSX = tonNhap.NamSX;
                    ton.Gia = tonNhap.Gia;                    
                    ton.SLTonKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));
                    ton.SLNhapKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));
                    //ton.SLTH = 0;//Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLTH"]));
                    //ton.SLSS = 0;//Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLSS"]));
                    ton.NgayTaoTon = DateTime.Now;
                    ton.SoTheKho = "";
                    ton.TangKeKhoang = "";
                    ton.ID_HopDong = tonNhap.ID_HopDong;

                    ton.ID_MatHang = m_ton.InsertNhap(ton);

                    tblSLNX objSLNX = new tblSLNX();
                    objSLNX.ID_MatHang = ton.ID_MatHang;
                    objSLNX.ID_DPNX = obj_PhieuNhap.ID_DPNX;
                    objSLNX.SLKH = ton.SLTonKH;
                    objSLNX.ThanhTien = ton.SLTonKH * ton.Gia;
                    objSLNX.GhiChu = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["GhiChu"]) ?? "");

                    m_SLNX.Insert(objSLNX);   
                }

                ////Xóa
                //if (status == 3)
                //{
                //    int ID_SLNX = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_SLNX"]));
                //    int ID_MatHang = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_MatHang"]));
                //    double SLKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));
                //    double SLTH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLTH"]));

                //    //1. Xóa SLNX
                //    m_SLNX.Delete(ID_SLNX);

                //    //2. Xóa trong Tồn (SLTonKH = SLTonKH - SLKH)
                //    m_ton.DeleteNhap(ID_MatHang, SLKH);

                //}
                ////Sửa
                //if (status == 2)
                //{
                //    int ID_SLNX = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_SLNX"]));
                //    int ID_MatHang = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns["ID_MatHang"]));
                //    CTon m_tonNhap = new CTon();
                //    tblTon tonNhap = m_tonNhap.SelectRecordByID(ID_MatHang);

                //    tblTon ton = new tblTon();
                //    ton.ID_VatTu = tonNhap.ID_VatTu;
                //    ton.MaKyHieu = tonNhap.MaKyHieu;
                //    ton.ID_Kho = Convert.ToInt32(lueKhoNhap.EditValue);
                //    ton.ID_CCL = tonNhap.ID_CCL;
                //    ton.ID_NguonVatTu = tonNhap.ID_NguonVatTu;
                //    ton.NamSX = tonNhap.NamSX;
                //    ton.Gia = tonNhap.Gia;
                //    ton.GiaUSD = tonNhap.GiaUSD;
                //    ton.SLTonKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));
                //    ton.SLNhapKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));
                //    //ton.SLTH = 0;//Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLTH"]));
                //    //ton.SLSS = 0;//Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLSS"]));
                //    ton.NgayTaoTon = DateTime.Now;
                //    ton.SoTheKho = tonNhap.SoTheKho;
                //    ton.TangKeKhoang = tonNhap.TangKeKhoang;
                //    ton.ID_HopDong = tonNhap.ID_HopDong;   

                //    tblSLNX objSLNX = m_SLNX.SelectRecordByID(ID_SLNX);
                //    //Update tblTon voi SLKH cũ và trả về ID_MatHang sau khi update
                //    ID_MatHang = m_ton.UpdateNhap(ton, Convert.ToDouble(objSLNX.SLKH));

                //    tblSLNX _SLNXNhap = new tblSLNX();
                //    _SLNXNhap.ID_SLNX = ID_SLNX;
                //    _SLNXNhap.ID_MatHang = ID_MatHang;
                //    _SLNXNhap.ID_DPNX = _ID_PhieuNhap;
                //    _SLNXNhap.SLKH = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["SLKH"]));
                //    _SLNXNhap.GhiChu = Convert.ToString(gridView1.GetRowCellValue(i, gridView1.Columns["GhiChu"]) ?? "");
                //    _SLNXNhap.ThanhTien = Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns["ThanhTien"]));
                //    m_SLNX.Update(_SLNXNhap);
                //}
            }

            KhoiTaoSLNX(_ID_PhieuXuat);
            KhoiTaoGiaoDien(true);            
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (_ID_PhieuXuat != 0)
            {
                KhoiTaoSLNX(0);
                KhoiTaoGiaoDien(true);
            }
            else
            {
                dtpNgayPhieu.Focus();
                KhoiTaoGiaoDien(false);
                Clear();
            }       
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            
            KhoiTaoSoPhieuXuat();
            KhoiTaoSoPhieuNhap();
            Clear();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (_ID_PhieuNhap != 0)
            {
                String SoPhieuNhap = txtSoPhieuNhap.Text;
                String SoPhieuXuat = txtSoPhieuXuat.Text;
                DialogResult dialogResult = MessageBox.Show("Bạn muốn xóa phiếu chuyển kho nội bộ" , "Chú ý", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    //Refesh phiếu
                    m_DPNX.Delete(_ID_PhieuNhap);
                    m_DPNX.Delete(_ID_PhieuXuat);
                    Clear();
                }
            }            
        }

        public void Clear()
        {           
            dtpNgayPhieu.EditValue = DateTime.Now;                  
            lueKhoXuat.EditValue = _ID_Kho;                   
            txtNguoiGiao.EditValue = "";
            txtNguoiNhan.EditValue = "";
            txtSoPhieuNhap.EditValue = "";
            txtSoPhieuXuat.EditValue = "";

            dtSLNX.Rows.Clear();
            _ID_PhieuNhap = 0;
            _ID_PhieuXuat = 0;            
        }

        public void KhoiTaoSoPhieuNhap()
        {
            CModule_Sy m_module = new CModule_Sy();
            txtSoPhieuNhap.EditValue = m_module.GetNewSoPhieu("N", DateTime.Now, "CK", ref _SoPhieuNhap);

            //if (DateTime.Now.Month.ToString().Length == 1)
            //    txtSoPhieuNhap.EditValue = Convert.ToString(GetSoPhieuNhap()) + "/0" + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString().Substring(2, 2);
            //else
            //    txtSoPhieuNhap.EditValue = Convert.ToString(GetSoPhieuNhap()) + "/" + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString().Substring(2, 2);
        }

        public void KhoiTaoSoPhieuXuat()
        {
            CModule_Sy m_module = new CModule_Sy();
            txtSoPhieuXuat.EditValue = m_module.GetNewSoPhieu("X", DateTime.Now, "CK", ref _SoPhieuXuat);

            //if (DateTime.Now.Month.ToString().Length == 1)
            //    txtSoPhieuXuat.EditValue = Convert.ToString(GetSoPhieuXuat()) + "/0" + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString().Substring(2, 2);
            //else
            //    txtSoPhieuXuat.EditValue = Convert.ToString(GetSoPhieuXuat()) + "/" + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString().Substring(2, 2);
        }

        //public int GetSoPhieuNhap()
        //{
        //    int q = 0;
        //    QLVTDataContext dc = new QLVTDataContext();

        //    q = Convert.ToInt32((from p in dc.tblDPNXes
        //                         where p.NX == "N" && Convert.ToDateTime(p.NgayPhieu).Year == DateTime.Now.Year && Convert.ToDateTime(p.NgayPhieu).Month == DateTime.Now.Month                                 
        //                         select p.SoPhieu).Max());
        //    return q + 1;
        //}

        //public int GetSoPhieuXuat()
        //{
        //    int q = 0;
        //    q = Convert.ToInt32((from p in dc.tblDPNXes
        //                         where p.NX == "X" && Convert.ToDateTime(p.NgayPhieu).Year == DateTime.Now.Year && Convert.ToDateTime(p.NgayPhieu).Month == DateTime.Now.Month
        //                         select p.SoPhieu).Max());

        //    return q + 1;
        //}

        public void KhoiTaoGiaoDien(Boolean f)
        {
            txtSoPhieuNhap.Properties.ReadOnly = true;
            txtSoPhieuXuat.Properties.ReadOnly = true;
            
            lueKhoXuat.Enabled = false;
            lueKhoNhap.Enabled = !f;
                        
            txtNguoiGiao.Properties.ReadOnly = f;
            txtNguoiNhan.Properties.ReadOnly = f;
            
            dtpNgayPhieu.Properties.ReadOnly = f;

            for (int i = 0; i < gridView1.Columns.Count; i++)
                gridView1.Columns[i].OptionsColumn.AllowEdit = false;


            gridView1.Columns["Del"].OptionsColumn.AllowEdit = !f;
            gridView1.Columns["GhiChu"].OptionsColumn.AllowEdit = !f;

            btnThem.Enabled = f;
            btnSua.Enabled = f;
            btnXoa.Enabled = f;
            btnGhi.Enabled = !f;
            btnHuy.Enabled = !f;
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName != "Status")
            {
                try
                {
                    if (Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Status"])) != 1)
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Status"], 2);
                }
                catch { }// bo qua loi them moi
            }

            if (e.Column.FieldName == "Del")
            {
                try
                {
                    Boolean Del = Convert.ToBoolean(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Del"]));
                    int Status = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Status"]));
                    if (Del == true)
                    {
                        if (Status == 1)
                            gridView1.DeleteRow(gridView1.FocusedRowHandle);
                        else
                            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Status"], 3);

                        gridView1.UpdateCurrentRow();
                    }
                    else
                    {
                        if (Status != 1)
                            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Status"], 2);
                        gridView1.UpdateCurrentRow();
                    }
                }
                catch { }
            }     
        }

        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                int Status = Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, gridView1.Columns["Status"]));
                if (Status == 1)//add new
                {
                    //e.Appearance.BackColor = Color.Blue;
                    //e.Appearance.BackColor2 = Color.SeaShell;
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
                }
                if (Status == 2)//update row
                {
                    //e.Appearance.BackColor = Color.MediumSpringGreen;
                    //e.Appearance.BackColor2 = Color.SeaShell;
                    e.Appearance.ForeColor = Color.Blue;
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Italic);
                }
                if (Status == 3)//delete row
                {
                    //e.Appearance.BackColor = Color.Yellow;
                    //e.Appearance.BackColor2 = Color.SeaShell;
                    e.Appearance.ForeColor = Color.Red;
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Strikeout);
                }
            }
        }

        private void lueKhoNhap_VisibleChanged(object sender, EventArgs e)
        {
            lueKhoNhap.Properties.PopulateColumns();
            lueKhoNhap.Properties.Columns["ID_Kho"].Visible = false;
            lueKhoNhap.Properties.Columns["ID_DonVi"].Visible = false;
            lueKhoNhap.Properties.Columns["KH_Kho"].Visible = false;            
        }
     
    }
}
